package ai_auto

import (
	"math/rand"

	"gddgame.cc/galaxy/common/dsl/game/ai"
)

//go:generate msgp

func MakeModelAi(commandList []*ai.AiCommandExt, config *AiGroupConfig) ai.ModelAi {
	return ai.ModelAi{
		Commands:      commandList,
		ScanRadius:    config.ScanRadius,
		PartnerRadius: config.PartnerRadius,
		AttackRadius:  config.AttackRadius,
	}
}

func NewAiCommand(command *AiCommand, conditions [][]*ai.AiConditionExt) *ai.AiCommandExt {
	ext := &ai.AiCommandExt{
		Command:    command.Command,
		RelationId: command.RelationId,
		Flag:       command.Flag,
		Conditions: conditions,
	}
	if len(command.Config.RelationIds) > 0 {
		ext.RelationId = command.Config.RelationIds[rand.Int31n(int32(len(command.Config.RelationIds)))]
	}
	return ext
}

func NewAiConditions(conditionTemplate *AiConditionTemplate) [][]*ai.AiConditionExt {
	conditions := make([][]*ai.AiConditionExt, len(conditionTemplate.Conditions))
	for index, value := range conditionTemplate.Conditions {
		conditions[index] = make([]*ai.AiConditionExt, len(value))
		for k, v := range value {
			conditions[index][k] = NewAiCondition(&v)
		}
	}
	return conditions
}

func NewAiCondition(condition *AiCondition) *ai.AiConditionExt {
	ext := &ai.AiConditionExt{
		Type:   condition.Type,
		Target: condition.Target,
		Select: condition.Select,
		Filter: condition.Filter,
		Number: condition.Number,
		Sort:   condition.Sort,
	}
	if condition.MaxValue == condition.MaxValue {
		ext.Value = condition.MaxValue
	} else {
		ext.Value = int(rand.Int31n(int32(condition.MaxValue-condition.MinValue+1))) + condition.MinValue
	}
	return ext
}
